import React from 'react';

import DBTable from 'components/DBTable';
import Logger from '../../../utils/Logger';

import ArticleManageInnerForm from './ArticleManageInnerForm';
import ArticleManageInnerTable from './ArticleManageInnerTable.js';

import {
  Form,
  Input,
  Row,
  Col,
  Button,
  DatePicker,
  Select,
  Icon,
  Radio,
  InputNumber,
  Checkbox,
  message,
  Upload,
  notification
} from 'antd';

const logger = Logger.getLogger('TreeDbTableView');
/**
 * 操作数据库中的一张表的组件, 又可以分为3个组件: 表单+表格+分页器
 */
class TreeDbTableView extends DBTable {

  constructor(props) {
    super(props);
    console.log('this.state',this.state);
  }

  componentWillReceiveProps = (nextProps) => {
  const params = this.props.requestParams;
  const currentParentId = params ? 
  (params.parentId ? params.parentId : params.catId) : null;
  const nextParentId = nextProps.requestParams ?
  (nextProps.requestParams.parentId ?
  nextProps.requestParams.parentId : nextProps.requestParams.catId ) : null;
  if (!nextParentId || (nextParentId == currentParentId)) {
    return;
  }
  logger.debug('receive new props and try to render, nextProps = %o', nextProps);
  this.tryFetchSchema(nextProps);  // 根据新的props重新获取配置

  this.state.queryObj = {};
  this.state.data = [];
  this.state.tableLoading = false;
  this.state.currentPage = 1;
  this.state.pageSize = 10;
  this.state.total = 0;
  // this.state.requestParams = nextProps.requestParams;
  this.setState({requestParams: nextProps.requestParams},()=>{
    this.refresh();
  });
  // 切换时也要重新查询一次数据
  }

  createInnerTable() {
    return ArticleManageInnerTable;
  }


  createInnerForm() {
    return ArticleManageInnerForm;
  }

  getInnerForm() {
    let form = null;
    if (this.tableConfig.showQueryFrom) {
      if (this.InnerNewFrom == null) {
        this.InnerNewFrom = Form.create({ onFieldsChange: this.getFormData })(this.createInnerForm());
      }
      const InnerNewFrom = this.InnerNewFrom;
      form = (<InnerNewFrom ref="getInnerFormData" requestParams={this.state.requestParams} parentHandleSubmit={this.handleFormSubmit} schema={this.querySchema} tableConfig={this.tableConfig}
        totalData={this.state.totalData} tableName={this.state.tableName} resetQuery={this.props.resetQuery} />);
    }
    return form;
  }

}

export default TreeDbTableView;
